// xc_lshop/shop/order/group/info.js
var actgroupbill = require('../../model/order_group')
// var actgroupbill = require('../../../business/model/business_bill')

var actbill = require('../../../../xc/model/pro_contents')
// var actbusinessbill = require('../../../business/model/business_bill')

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    auth:"common",//页面权限 common普通 business商家
    opstatus: "infor" ,//页面状态 infor拼团详情 invite邀请
    owngroupstatus:"-1"//自团状态
  },

  //邀请好友
  invite() {
    this.selectComponent("#widgetsshare").showSharefunc();
  },
  //拼单成功操作
  opsuccess(e){
    var that=this;
    var id=this.data.xcmodel.id
    actgroupbill.successgroup(id).then(res => {
      if (res["status"] == 1) {
        wx.showToast({
          icon: 'success',
          title: "拼单成功",
        })
        that.getmodel(id)
      } else {
        wx.showToast({
          icon: 'error',
          title: res["message"],
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  xcloaddata: function (options, config) {
    var that = this;
    
    var updateconfig={};
    if (config.prothem && config.prothem.detail) {
      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      updateconfig['popconfig'] = popconfig
      updateconfig['popCustomStyle'] = popCustomStyle;
    }
    if (config.order) {
      var owngroupstatus = config.order.owngroupstatus
      updateconfig['owngroupstatus'] = owngroupstatus
    }
    that.setData(updateconfig)
    var groupid = options.groupid;
    that.getmodel(groupid)

   
  },
  buyop: function (ev) {
    if(!this.data.memberinfo){
      this.afterlogin();
      return;
    }
    // //活动未开始/结束不能购买 达到上限
    // if (this.data.activityid && (this.data.actstatus == -1 || this.data.actstatus == 2 || this.data.xcmodel.limitinfo.limitnum == 0)) {
    //   return;
    // }
    //后台设置能否自己拼团
    // if(this.data.owngroupstatus!=1){
    //   var users=ev.currentTarget.dataset.users;
    //   if(users.includes(this.data.memberinfo.id)){
    //     wx.showToast({
    //       icon:"error",
    //       title: '不可重复参团',
    //     })
    //      return ;
    //   }
    // }
    var that = this
    // var option = "";
    var option = {
      aid: this.data.xcmodel.aid,
      atype: 2
    }
    // if (this.data.activityid && this.data.activitytype) {
    //   option = {
    //     aid: this.data.activityid,
    //     atype: this.data.activitytype
    //   }
    // }
    // if(ev.currentTarget.dataset.pid){
    //   var pid=ev.currentTarget.dataset.pid;
    //   if(pid==1){
    //     option = {
    //       aid:0,
    //       atype: 0
    //     }
    //   }
    // }
    if(ev.currentTarget.dataset.groupid){
      option['groupid']=ev.currentTarget.dataset.groupid
    }
    console.log(that.data.selected)
    actbill.showattrchose(that, this.data.xcmodel.proid, that.data.selected, ev.currentTarget.dataset.op, option)
  },
  attrchoose: function (e) {
    var choosed = e.detail;
    if (choosed.proid == this.data.xcmodel.proid) {
      var choosedinfor = "";
      if (e.detail.attrs) {
        for (var i in e.detail.attrs) {
          if (choosed.attrs[i].keyname != '') {
            choosedinfor += "【" + choosed.attrs[i].keyname.name + "】";
          }
        }
      }
     

      this.setData({
        selected: e.detail,
        choosedinfor: choosedinfor
      })

    }
  },
  getmodel: function (id) {
    var that = this;
    actgroupbill.getmodel(id).then(res => {
      if (res.status == 1) {
        var obj = res.obj;
        if (obj['proinfo']['skudata'] && obj['proinfo']['skudata']['stockdata']) {
          var stockdata = obj['proinfo']['skudata']['stockdata'];
          var minactprice = "";
          for (var i in stockdata) {
            if (!minactprice || minactprice > stockdata[i].activityprice) {
              minactprice = stockdata[i].activityprice
            }
          }
          obj['proinfo']['minactprice'] = minactprice;
        }
        if(that.data.memberinfo){
          that.changeopstatus(that.data.memberinfo,obj.leaderid)
        }
        that.setData({
          xcmodel: obj
        })
       

      }
    })
  },
  //倒计时结束
  ontimeFinish(e) {
    var that = this;

    that.getmodel(that.data.xcmodel.id)
  },
  afterlogin: function (options, config, userinfo) {
    var app = getApp()
    var that = this;

    app.xc_sys_config.authop.oppower("login").then(function (res) {
      var member = app.globalData.member;
      var memberinfo=member.memberinfo

      var update = {};
      update['memberinfo']=memberinfo;
      if(options.auth){
        if(options.auth=='business' && memberinfo.authority==2){
          that.setData({
            auth:'business'
           })
        }
      }
      // that.setData({
      //   "member": app.globalData.member
      // })
      // console.log(memberinfo)
      // if (memberinfo.id == that.data.xcmodel.leaderid && that.data.opstatus == "invite") {
      //   update['opstatus'] = 'infor'
      // } else if (memberinfo.id != that.data.xcmodel.leaderid && that.data.opstatus == "infor") {
      //   update['opstatus'] = 'invite'
      // }
      that.setData(update)
      if(that.data.xcmodel){
        that.changeopstatus(memberinfo,that.data.xcmodel.leaderid)
      }
    })
  },
  changeopstatus(memberinfo,leaderid){
    var that=this;
    var update = {};
    if (memberinfo.id == leaderid && that.data.opstatus == "invite") {
      update['opstatus'] = 'infor'
    } else if (memberinfo.id != leaderid && that.data.opstatus == "infor") {
      update['opstatus'] = 'invite'
    }
    that.setData(update)
  }

  
})